package com.example.administrator.yuejian_2.activity;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.administrator.yuejian_2.R;
import com.example.administrator.yuejian_2.adapter.SearchAdapter;
import com.example.administrator.yuejian_2.bean.ActivitySearchBean;
import com.example.administrator.yuejian_2.interservice.ActivityService;
import com.example.administrator.yuejian_2.interservice.Const;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchActivity extends AppCompatActivity {
    private ImageButton backButton;
    private ImageButton searchButton;
    private EditText editText;
    private RecyclerView recyclerView;
    private SearchAdapter adapter;

    private String name;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        backButton = (ImageButton) findViewById(R.id.search_back);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });


        editText = (EditText) findViewById(R.id.search_editText);
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                name = editText.getText().toString();
                getresult();

            }
        });

        searchButton = (ImageButton) findViewById(R.id.search_imageButton8);
        recyclerView = (RecyclerView) findViewById(R.id.search_recyclerView);

    }

    private void getresult(){
        context = SearchActivity.this;
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Const.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ActivityService service = retrofit.create(ActivityService.class);
        Call<List<ActivitySearchBean>> call = service.searchactivity(name);
        call.enqueue(new Callback<List<ActivitySearchBean>>() {
            @Override
            public void onResponse(Call<List<ActivitySearchBean>> call, Response<List<ActivitySearchBean>> response) {
                if (response.body() != null){
                    adapter = new SearchAdapter(context,response.body());
                    LinearLayoutManager layoutManager = new LinearLayoutManager(context);
                    layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                    recyclerView.setAdapter(adapter);
                    recyclerView.setLayoutManager(layoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());

                }
            }

            @Override
            public void onFailure(Call<List<ActivitySearchBean>> call, Throwable t) {

            }
        });
    }
}
